package io.github.syske.springbootjdbcmybatisdemo.service.impl;

import io.github.syske.springbootjdbcmybatisdemo.dao.LocateDao;
import io.github.syske.springbootjdbcmybatisdemo.dao.MallDao;
import io.github.syske.springbootjdbcmybatisdemo.dao.TPDao;
import io.github.syske.springbootjdbcmybatisdemo.entity.Locate;
import io.github.syske.springbootjdbcmybatisdemo.entity.vo.MallVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MallLocateServiceImpl {

    @Autowired
    MallDao mallDao;

    @Autowired
    LocateDao locateDao;

    @Autowired
    TPDao tpDao;

    public MallVO getMallByRfid(String rfid){
        System.out.println("rfid是:"+rfid);
        Map<String,Object> param = new HashMap<>();
        param.put("m_rfid",rfid);
        List<MallVO> bindMall = mallDao.isBindMall(param);
        if(bindMall !=null && bindMall.size()>0){
            MallVO mallVO = bindMall.get(0);
            param.put("tp_rfid",rfid);
            Locate locate = locateDao.getLocateByRfid(param);
            if(locate == null){
                List<Locate> emptyLocate = locateDao.getEmptyLocate2();
                Locate tempLocate = findLowerLocate(emptyLocate);
                System.out.println("空货位是:"+tempLocate.locate);
                MallVO empty= new MallVO();
                empty.locate_area = tempLocate.locate_area;
                empty.locate_type = tempLocate.locate_type;
                empty.locate = tempLocate.locate;
                empty.is_binding = false;
                empty.rfid = rfid;
                return empty;
            }
            mallVO.locate_area = locate.locate_area;
            mallVO.locate_type = locate.locate_type;
            mallVO.locate = locate.locate;
            mallVO.locate_status =   locate.locate_status;
            mallVO.box_num = locate.box_num;
            mallVO.is_binding = true;
            mallVO.rfid = rfid;
            mallVO.item_code = mallVO.m_type;
            mallVO.item_name = mallVO.m_type;

            return mallVO;
        }else {
            List<Locate> emptyLocate = locateDao.getEmptyLocate2();
            Locate locate = findLowerLocate(emptyLocate);
            System.out.println("空货位是:"+locate.locate);
            MallVO empty= new MallVO();
            empty.locate_area = locate.locate_area;
            empty.locate_type = locate.locate_type;
            empty.locate = locate.locate;
            empty.is_binding = false;
            empty.rfid = rfid;
            return empty;
        }

    }

    private Locate findLowerLocate(List<Locate> emptyLocate2){
        List<Locate> collect = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-01") > 0)
                .collect(Collectors.toList());
        if(collect!=null && collect.size()>0) {
            return collect.get(0);
        }else {
            List<Locate> collect2 = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-02") > 0)
                    .collect(Collectors.toList());
            if(collect2!=null && collect2.size()>0) {
                return collect2.get(0);
            }else {
                List<Locate> collect3 = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-03") > 0)
                        .collect(Collectors.toList());
                if(collect3!=null && collect3.size()>0){
                    return collect3.get(0);
                }else {
                    List<Locate> collect4 = emptyLocate2.stream().filter(locate -> locate.locate.indexOf("-04") > 0)
                            .collect(Collectors.toList());
                    if(collect4!=null && collect4.size()>0){
                        return collect4.get(0);
                    }else {
                        return emptyLocate2.get(0);
                    }
                }
            }
        }
    }

}
